/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.serverpackets;

import java.util.List;

import javolution.util.FastList;

import jmmos.gameserver.model.actor.instance.MMOMerchantInstance;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.model.items.instance.MMOItemInstance;

/**
 * This class ...
 * @version $Revision: 1.4.2.3.2.4 $ $Date: 2005/03/27 15:29:39 $
 */
public class SellList extends MMOGameServerPacket
{
	private final MMOPcInstance _activeChar;
	private final MMOMerchantInstance _lease;
	private final long _money;
	private final List<MMOItemInstance> _selllist = new FastList<>();
	
	public SellList(MMOPcInstance player)
	{
		_activeChar = player;
		_lease = null;
		_money = _activeChar.getGold();
		doLease();
	}
	
	public SellList(MMOPcInstance player, MMOMerchantInstance lease)
	{
		_activeChar = player;
		_lease = lease;
		_money = _activeChar.getGold();
		doLease();
	}
	
	private void doLease()
	{
		if (_lease == null)
		{
			for (MMOItemInstance item : _activeChar.getInventory().getItems())
			{
				if (!item.isEquipped() && item.isSellable() && (!_activeChar.hasSummon() || (item.getObjectId() != _activeChar.getSummon().getControlObjectId()))) // Pet is summoned and not the item that summoned the pet
				{
					_selllist.add(item);
				}
			}
		}
	}
	
	@Override
	protected final void writeImpl()
	{
		writeC(0x06);
		writeQ(_money);
		writeD(_lease == null ? 0x00 : 1000000 + _lease.getTemplate().getNpcId());
		writeH(_selllist.size());
		
		for (MMOItemInstance item : _selllist)
		{
			writeH(item.getItem().getType1());
			writeD(item.getObjectId());
			writeD(item.getDisplayId());
			writeQ(item.getCount());
			writeH(item.getItem().getType2());
			writeH(item.isEquipped() ? 0x01 : 0x00);
			writeD(item.getItem().getBodyPart());
			writeH(item.getEnchantLevel());
			writeH(0x00); // TODO: Verify me
			writeH(item.getCustomType2());
			writeQ(item.getItem().getReferencePrice() / 2);
			
			// T1
			writeH(item.getAttackElementType());
			writeH(item.getAttackElementPower());
			for (byte i = 0; i < 6; i++)
			{
				writeH(item.getElementDefAttr(i));
			}
			// Enchant Effects
			for (int op : item.getEnchantOptions())
			{
				writeH(op);
			}
		}
	}
}
